package com.sks.web.action.house;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.stereotype.Controller;

import com.sks.bean.pojo.Comment;
import com.sks.service.house.CommentService;
import com.sks.service.house.HouseService;
import com.sks.service.privilege.UsersService;
import com.sks.utils.StringUtil;
import com.sks.utils.WebUtil;
import com.sks.web.action.BasicAction;
import com.sks.web.action.privilege.Permission;
import com.sks.web.formbean.house.HouseForm;

@Controller("/user/house/comment")
public class UserCommentAction extends BasicAction {
	
	@Resource(name = "houseServiceBean") private HouseService service;
	@Resource(name = "commentServiceBean") private CommentService commentService;
	@Resource UsersService usersService;
	
	@Permission(module = "employee", privilege = "viewComment")
	public ActionForward add(ActionMapping mapping, ActionForm form,
	         HttpServletRequest request, HttpServletResponse response)
	         throws Exception {
		HouseForm formbean = (HouseForm) form;
		String menuName = request.getParameter("menuName");
		Comment com = new Comment();
		com.setHouse(service.find(formbean.getHouseId()));
		com.setUsers(WebUtil.getUsers(request));
		com.setPublishTime(new Date());
		
		ActionForward forward = new ActionForward("/sks/house.do?method=info&menuName=" + menuName + "&houseId=" + formbean.getHouseId() + "#comment", true);
		if (StringUtil.isEmpty(formbean.getContent())) {
			return forward;
		}
		com.setContent(StringUtil.htmlEscape(formbean.getContent()));
		commentService.save(com);
		return forward;
	}
	
	@Override
	protected ActionForward validateToken(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response, String methodName) throws Exception {
		return null;
	}
}
